<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */

namespace Modules\Admin\Http\Controllers;

use Illuminate\Support\Facades\Request;
use Modules\Admin\Service\OrderRepairstoreService;
use Excel;
use HiCommon\Service\AutoScheduleService;

class OrderRepairstoreController extends BaseController
{
    /**
     *  服务商品订单列表
     */
    public function index()
    {
        $arr_search = (array)Request::all();
        $res = OrderRepairstoreService::get_list((array)$arr_search);
        echo_error($res);
        return view('admin::order.repair.index', $res['data']);
    }

    /**
     * 保养订单列表
     */
    public function maintain_order_list()
    {
        $arr_search = (array)Request::all();
        $res = OrderRepairstoreService::maintain_order_list((array)$arr_search);
        echo_error($res);
        return view('admin::order.maintain.index', $res['data']);
    }

    /**
     * 服务订单详情页面
     */
    public function detail()
    {
        $nId = Request::get('id', 0);
        $res = OrderRepairstoreService::get_one((int)$nId);
        // echo_error($res);

        $arr_data = [
            'arr_data' => $res['data'],
        ];
        return view('admin::order.repair.detail', $arr_data);
    }

    /**
     * 修改 XXX 页面操作
     */
    public function update()
    {
        $arr_input = Request::input();
        $arr_input['operator_id'] = (int)admin_user_id();
        return OrderRepairstoreService::edit_one($arr_input);
    }


    /**
     * 更改 服务订单 状态
     */
    public function changStatus()
    {
        $arr_input = Request::all();

        shiwu_start();
        $res = OrderRepairstoreService::change_one_state($arr_input);
        shiwu_end($res);

        return $res;
    }

    /**
     * 删除 服务订单
     */
    public function del()
    {
        $n_id = (int)Request::get('id', 0);

        return OrderRepairstoreService::delete_one($n_id);
    }

    /**
     * 导出Excel
     */
    public function export()
    {
        $arr_search = (array)Request::all();
        $orderList = OrderRepairstoreService::get_order_list((array)$arr_search);
        //dd($orderList);
        $title = $arr_search['cate'] == '1' ? '服务订单' : '保养订单';
        $cellLetter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

        Excel::create($title, function ($excel) use ($orderList, $cellLetter) {
            foreach ($orderList as $sheet_name => $item) {
                $cellData = [];
                $cellData[0] = ['订单编号', '订单状态', '客户电话', '订单金额', '优惠金额', '支付金额', '支付方式', '支付时间', '下单时间'];

                foreach ($item as $key => $value) {
                    $cellData[$key + 1] = [
                        $value['order_number'],   //订单编号
                        $value['order_status'],   //订单状态
                        $value['user']['phone'],  //客户电话
                        $value['order_price'],    //订单金额
                        $value['coupon_price'],   //优惠金额
                        $value['pay_price'],      //支付金额
                        $value['pay_type'],       //支付方式
                        $value['pay_time'],       //支付时间
                        $value['created_at'],     //下单时间
                    ];
                }

                $excel->sheet($sheet_name, function ($sheet) use ($cellData, $cellLetter) {
                    $sheet->rows($cellData);
                    $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
                    $sheet->getStyle('A1:L' . count($cellData))->getAlignment()->setVertical('center');//垂直居中
                    $sheet->getStyle('A1:L1')->applyFromArray([
                        'font' => ['bold' => true, 'color' => ['rgb' => '0072ff']]//字体设置
                    ]);
                    $sheet->getStyle('A1:L' . count($cellData))->applyFromArray([
                        'alignment' => ['horizontal' => 'center']
                    ]);

                    for ($i = 0; $i <= count($cellData[0]); $i++) {
                        $cell = $cellLetter[$i];
                        $width = 25;
                        $sheet->setWidth($cell, $width);
                    }
                });

                $excel->setactivesheetindex(0);//设置默认打开第一个sheet
            }
        })->export('xls');
    }

}